//
// Created by Yun on 2025/2/26.
//

#include "bsp_tim.h"


/* TIM设备池 */
#define MAX_UART_NUM 8
Tim_Hw_Cfg_TypeDef* stTimHwCfgPool[MAX_UART_NUM] = {NULL};
uint8_t u8TimNum = 0;

uint8_t Bsp_Tim_Init(void* pTimHwCfg, pfNVIC_Callback_TypeDef pfTimCallback)
{
    if(!pTimHwCfg || u8TimNum >= MAX_UART_NUM || !pfTimCallback) return BSP_ERROR;
    Tim_Hw_Cfg_TypeDef* pstTimHwCfg = (Tim_Hw_Cfg_TypeDef*)pTimHwCfg;

    pstTimHwCfg->pfTimCallback = pfTimCallback;

    if     (pstTimHwCfg->stHTim.Instance == TIM1) __HAL_RCC_TIM1_CLK_ENABLE();
    else if(pstTimHwCfg->stHTim.Instance == TIM2) __HAL_RCC_TIM2_CLK_ENABLE();
    else if(pstTimHwCfg->stHTim.Instance == TIM3) __HAL_RCC_TIM3_CLK_ENABLE();
    else if(pstTimHwCfg->stHTim.Instance == TIM4) __HAL_RCC_TIM4_CLK_ENABLE();
    else if(pstTimHwCfg->stHTim.Instance == TIM5) __HAL_RCC_TIM5_CLK_ENABLE();
    else if(pstTimHwCfg->stHTim.Instance == TIM6) __HAL_RCC_TIM6_CLK_ENABLE();
    else if(pstTimHwCfg->stHTim.Instance == TIM7) __HAL_RCC_TIM7_CLK_ENABLE();
    else if(pstTimHwCfg->stHTim.Instance == TIM8) __HAL_RCC_TIM8_CLK_ENABLE();



    if(HAL_TIM_Base_Init(&pstTimHwCfg->stHTim)!= HAL_OK) return BSP_ERROR;




    HAL_NVIC_SetPriority(TIM6_IRQn, pstTimHwCfg->u8ITPriority, pstTimHwCfg->u8ITSubPriority);
    HAL_NVIC_EnableIRQ(TIM6_IRQn);

    stTimHwCfgPool[u8TimNum++] = pstTimHwCfg;

    Bsp_Tim_Enable(pstTimHwCfg, 1);

    return BSP_OK;
}


uint8_t Bsp_Tim_Enable(void* pTimHwCfg, uint8_t u8Enable)
{
    if(!pTimHwCfg) return BSP_ERROR;
    Tim_Hw_Cfg_TypeDef* pstTimHwCfg = (Tim_Hw_Cfg_TypeDef*)pTimHwCfg;

    switch(u8Enable)
    {
        case 0:
            if (HAL_TIM_Base_Stop_IT(&pstTimHwCfg->stHTim) != HAL_OK) return BSP_ERROR;
            return BSP_OK;
        case 1:
            if (HAL_TIM_Base_Start_IT(&pstTimHwCfg->stHTim) != HAL_OK) return BSP_ERROR;
            return BSP_OK;
    }
    return BSP_ERROR;
}

/* -------------------- TIM中断处理函数  --------------------*/

/*!
 * @brief 串口中断通用处理函数
 * @param pstHwCfg 串口配置
 * @note 该函数处理串口中断，包括接收中断、空闲中断、发送中断
 */
static void Bsp_Tim_IRQHandler(Tim_Hw_Cfg_TypeDef *pstTimHwCfg)
{
    /* 参数检查 */
    if(pstTimHwCfg == NULL) return;
    /* 中断处理 */
    __HAL_TIM_CLEAR_FLAG(&pstTimHwCfg->stHTim, TIM_FLAG_UPDATE);

    pstTimHwCfg->pfTimCallback(pstTimHwCfg);
}

/*!
 * @brief 找到串口配置
 * @param pTim 串口设备号
 * @return 串口配置
 * @note 该函数根据传入的串口设备号，在设备池中找到对应的串口配置
 */
static  Tim_Hw_Cfg_TypeDef* Bsp_Find_Tim_Hw_Cfg(TIM_TypeDef* pTim)
{
    uint8_t u8Idx = 0;
    Tim_Hw_Cfg_TypeDef *pstHwCfg = NULL;

    /* 遍历设备池 */
    for (u8Idx = 0; u8Idx < u8TimNum; u8Idx++)
    {
        pstHwCfg = stTimHwCfgPool[u8Idx];
        if (pstHwCfg->stHTim.Instance == pTim) break;
    }
    return pstHwCfg;
}


#define TIM_IRQ(TIMx_IRQ_FUNC, TIMx) \
void TIMx_IRQ_FUNC(void)               \
{                                       \
    Tim_Hw_Cfg_TypeDef *pstHwCfg = Bsp_Find_Tim_Hw_Cfg(TIMx);\
    Bsp_Tim_IRQHandler(pstHwCfg); \
}\

TIM_IRQ(TIM1_IRQHandler, TIM1)
TIM_IRQ(TIM2_IRQHandler, TIM2)
TIM_IRQ(TIM3_IRQHandler, TIM3)
TIM_IRQ(TIM4_IRQHandler, TIM4)
TIM_IRQ(TIM5_IRQHandler, TIM5)
TIM_IRQ(TIM6_IRQHandler, TIM6)
TIM_IRQ(TIM7_IRQHandler, TIM7)
TIM_IRQ(TIM8_IRQHandler, TIM8)

// 实现中断回调函数
//void TIM6_IRQHandler(void)
//{
//    Tim_Hw_Cfg_TypeDef *pstHwCfg = Bsp_Find_Tim_Hw_Cfg(TIM6);
//    Bsp_Tim_IRQHandler(pstHwCfg);
//}


Tim_Interface_TypeDef stBspTimInterface = {
        .Init = Bsp_Tim_Init,
       .Enable = Bsp_Tim_Enable
};
